<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    //out.print(path);
%>
<html>
<head>
    <meta charset="utf-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <!-- 用户名 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名</label>
        <div class="layui-input-block">
            <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" class="layui-input">
            <tip>字母数字组合，开头是字母，3-15个字符</tip>
        </div>
    </div>

    <!-- 密码 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">密码</label>
        <div class="layui-input-block">
            <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" class="layui-input">
            <tip>密码应为6-20个字符，由数字和字母组成</tip>
        </div>
    </div>

    <!-- 邮箱 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="required|email" lay-reqtext="邮箱不能为空" placeholder="请输入邮箱" class="layui-input">
            <tip>请确保输入有效的邮箱地址</tip>
        </div>
    </div>

    <!-- 手机号码 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">手机号码</label>
        <div class="layui-input-block">
            <input type="text" name="phone" lay-verify="required|phone" lay-reqtext="手机号码不能为空" placeholder="请输入手机号码" class="layui-input">
        </div>
    </div>

    <!-- 地址 -->
    <div class="layui-form-item">
        <label class="layui-form-label required">地址</label>
        <div class="layui-input-block">
            <input type="text" name="address" lay-verify="required" lay-reqtext="地址不能为空" placeholder="请输入地址" class="layui-input">
        </div>
    </div>

    <!-- 用户状态 -->
    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="status">
                <option value="active">启用</option>
                <option value="inactive">禁用</option>
            </select>
        </div>
    </div>

    <!-- 确认保存按钮 -->
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>

<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        // 监听提交
        form.on('submit(saveBtn)', function (data) {
            var jsonData = JSON.stringify(data.field);
            console.log(jsonData);
            $.post('${pageContext.request.contextPath}/userServlet?method=addUser', {"data": jsonData}, function (response) {
                if (response == 1) {
                    layer.msg("添加成功", {icon: 1}, function () {
                        window.location = 'findUser.jsp';
                    });
                } else {
                    layer.msg("添加失败", {icon: 2});
                }
            }, "json");

            return false;
        });
    });
</script>
</body>
</html>
